package com.tankbattle.model;

import com.tankbattle.util.ImageManager;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

/**
 * 子弹类，继承自GameObject
 */
public class Bullet extends GameObject {
    private Direction direction;  // 子弹方向
    private int speed;           // 子弹速度
    private boolean fromPlayer;  // 是否来自玩家
    
    public Bullet(int x, int y, Direction direction, boolean fromPlayer) {
        // 子弹大小为16x16，与图片尺寸一致
        super(x, y, 16, 16);
        this.direction = direction;
        this.fromPlayer = fromPlayer;
        this.speed = 8; // 子弹速度比坦克快
    }
    
    @Override
    public void update(double delta) {
        // 根据方向移动子弹
        switch (direction) {
            case UP:
                y -= speed;
                break;
            case DOWN:
                y += speed;
                break;
            case LEFT:
                x -= speed;
                break;
            case RIGHT:
                x += speed;
                break;
        }
    }
    
    /**
     * 检查子弹是否超出屏幕边界
     * @param screenWidth 屏幕宽度
     * @param screenHeight 屏幕高度
     * @return 如果子弹超出边界返回true，否则返回false
     */
    public boolean isOutOfBounds(int screenWidth, int screenHeight) {
        return x < -width || y < -height || x > screenWidth || y > screenHeight;
    }
    
    @Override
    public void draw(Graphics2D g) {
        if (!active) return;

        // 根据子弹方向获取对应图片
        String imageKey = "bullet_" + direction.toString().toLowerCase() + ".png";
        BufferedImage bulletImage = ImageManager.getInstance().getImage(imageKey);
        if (bulletImage != null) {
            g.drawImage(bulletImage, x, y, width, height, null);
        }
    }

    // Getter 和 Setter
    public Direction getDirection() {
        return direction;
    }
    
    public boolean isFromPlayer() {
        return fromPlayer;
    }

    public boolean isActive() {
        return active;
    }

    public void setActive(boolean active) {
        this.active = active;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }
}
